# Build recipe for isapnptools.
#
# Copyright (c) 2017-2018, 2021 Matias Fonzo, <selk@dragora.org>.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# Exit immediately on any error
set -e

program=isapnptools
version=1.27
release=5

# Define a category for the output of the package name
pkgcategory=tools

tarname=${program}-${version}.tgz

# Remote source(s)
fetch=http://www.roestockfox.co.uk/files/isapnptools/$tarname

description="
ISA plug and play configuration utility.

Two programs, one allows the dumping of resource data and generation of
a skeleton configuration file, the other configures ISA PnP hardware
using a configuration file.
"

homepage=https://www.roestockfox.co.uk/isapnptools/index.html
license=GPLv2+

# Source documentation
docs="AUTHORS COPYING ChangeLog NEWS README"
docsdir="${docdir}/${program}-${version}"

build()
{

    # Check architecture
    case $arch in
    i?86)
        true
        ;;
    *)
        echo "isapnptools is for the x86 platform-only ..." 1>&2
        return 0
        ;;
    esac

    unpack "${tardir}/$tarname"

    cd "$srcdir"

    # Set sane permissions
    chmod -R u+w,go-w,a+rX-s .

    # Fix compilation on musl
    patch -p1 < "${worktree}/patches/isapnptools/isapnptools-musl-2.patch"

    # Update for hosts based on musl
    cp -f "${worktree}/archive/common/config.guess" config.guess
    cp -f "${worktree}/archive/common/config.sub" config.sub

    ./configure CPPFLAGS="$QICPPFLAGS" CFLAGS="$QICFLAGS -fcommon" LDFLAGS="$QILDFLAGS -static" \
     $configure_args \
     --libdir=/usr/lib${libSuffix} \
     --mandir=$mandir \
     --disable-realtime \
     --disable-pci \
     --build="$(gcc -dumpmachine)"

    make -j${jobs} V=1
    make -j${jobs} DESTDIR="$destdir" install

    # Compress and link man pages (if needed)
    if test -d "${destdir}/$mandir"
    then
        (
            cd "${destdir}/$mandir"
            find . -type f -exec lzip -9 {} +
            find . -type l | while read -r file
            do
                ln -sf "$(readlink -- "$file").lz" "${file}.lz"
                rm -- "$file"
            done
        )
    fi

    # Copy documentation
    mkdir -p "${destdir}/$docsdir"
    cp -p $docs "${destdir}/$docsdir"
}

